'use strict';

stareal
    .controller("ReBindController", function ($scope, $api, $stateParams, $alert, $document, localStorageService, $state, $interval) {
        $scope.good_id=$stateParams.good_id;
        $scope.telphone_no = "";
        $scope.accessToken = "";
        $scope.password = "";
        $scope.password2 = "";
        $scope.paracont = "获取验证码";
        $scope.code = "";
        $scope.codeStatus=true;
        $scope.registerStatus=false;
        $scope.codeKey=localStorageService.get('sendCodeStatus');
        if($scope.codeKey==1){
            $scope.registerStatus=true;
        }
        $("#login_registercontainer").height($(window).height());
        $("#tanCode_container").height($(window).height());
        // $("#agreement_radio").click(function(){
        //     if($(this).hasClass("checked")){
        //         $(this).removeClass("checked");
        //     }else{
        //         $(this).addClass("checked");
        //     }
        // })
        //清除
        $scope.clearNumber=function(){
            $scope.telphone_no = "";
        }
        //前往填写验证码
        $scope.goToRgister=function(){
            if (!$scope.login.validatemobile($scope.telphone_no)){
                return;
            }
            $("#tanCode_container").show();
            var capOption = {callback:cbfn, showHeader:false,themeColor:'e72e62',type:"popup"};
            capDestroy();
            capInit(document.getElementById("TCaptcha"),capOption);
            //回调函数：验证码页面关闭时回调
            function cbfn(retJson) {
                console.log(retJson);
                if (retJson.ret == 0) {
                    // 用户验证成功
                    var ticket= retJson.ticket;
                    // document.getElementById("myform").submit();
                    $("#tanCode_container").hide();
                    $api.post("app/slidingVerificationCode/verify",{ticket:ticket,mobile:$scope.telphone_no},true)
                        .then(function (ret){
                            console.log(ret)
                            if (ret.retCode == "0"){
                                // $("#tanCode_container").hide();
                                localStorageService.set('code_token', ret.accessToken);
                                $state.go('main.rebindCode',{good_id:$scope.good_id,mobile:$scope.telphone_no},true);
                                // $scope.codeStatus=false;
                            }else{
                                $alert.show("发送短信验证码失败!");
                            }
                        },function(err){
                            $alert.show(err);
                        });
                }else if(retJson.ret ==2){
                    //用户关闭验证码页面，没有验证
                    $("#tanCode_container").hide();
                }else{
                    $alert.show("验证码已发送!");
                }
            }
        }

        $scope.goToNext=function(){
            if (!$scope.login.validatemobile($scope.telphone_no)){
                return;
            }
            $state.go('main.rebindmyCode',{good_id:$scope.good_id,mobile:$scope.telphone_no},true);
        }
        $scope.login = {
            validatemobile:function (mobile){
                if(!mobile){
                    $alert.show("请输入手机号")
                    return false
                }
                if (mobile.toString().length == 0) {
                    $alert.show('请输入手机号码！');
                    return false;
                }
                if (mobile.toString().length != 11) {
                    $alert.show('请输入11位手机号码！');
                    return false;
                }
                var myreg = /^1[3|4|5|6|7|8|9][0-9]{9}$/; //验证规则
                if (!myreg.test(mobile)) {
                    $alert.show('请输入有效的手机号码！');
                    return false;
                }
                return true;
            }
        }
    });